package com.boc.ewr.risk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.risk.entity.HeadlessCac;
import com.boc.ewr.risk.entity.vo.CacSameMobCusmNoVo;
import com.boc.ewr.risk.entity.vo.SamePersonMuchAcctVo;
import com.boc.ewr.risk.service.ICacRiskService;
import com.boc.ewr.risk.service.IHeadlessCacService;
import com.boc.ewr.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author : Wzz
 * @create : 2023-06- 01- 16:29
 * @desc : 风险预警-涉及Cac的风险预警清单
 * 同手机号存在不同客户号下
 * 同法人、财务负责人拥有3个及以上的客户号
 * 无头账户监控
 */
@Api(tags = "风险预警-涉及Cac的风险预警清单")
@RestController
@RequestMapping("/risk/cacRisk")
@Slf4j
public class CacRiskController extends JeecgController<Cac, ICacRiskService> {

    @Autowired
    private IHeadlessCacService headlessCacService;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 同手机号存在不同客户号下  分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "风险预警-同手机号存在不同客户号下")
    @ApiOperation(value = "风险预警-同手机号存在不同客户号下", notes = "风险预警-同手机号存在不同客户号下")
    @GetMapping(value = "/genSamePhoneNum")
    public Result<IPage<Cac>> genSamePhoneNum(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        List<Cac> cacSameMobCusmNoVos = service.judgeSamePhoneNum();

        return Result.OK(PageUtil.create(cacSameMobCusmNoVos, pageNo, pageSize));
    }

    /**
     * 同手机号存在不同客户号下 导出excel
     */
    //@RequiresPermissions("com.boc.ewr:cac:exportXls")
    @RequestMapping(value = "/exportXls/samePhoneNum")
    public ModelAndView exportSamePhoneNumXls() {
        String title = "同手机号存在不同客户号下";

        List<Cac> cacSameMobCusmNoVos = service.judgeSamePhoneNum();

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, Cac.class);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + this.getLoginUser().getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, cacSameMobCusmNoVos);
        return mv;
    }


    /**
     * 同法人、财务负责人拥有3个及以上的客户号  分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "风险预警-同法人、财务负责人拥有3个及以上的客户号")
    @ApiOperation(value = "风险预警-同法人、财务负责人拥有3个及以上的客户号", notes = "风险预警-同法人、财务负责人拥有3个及以上的客户号")
    @GetMapping(value = "/genSamePersonMuchAcct")
    public Result<IPage<SamePersonMuchAcctVo>> genSamePersonMuchAcct(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        List<SamePersonMuchAcctVo> samePersonMuchAcctVos = service.judgeSamePersonMuchAcct();

        return Result.OK(PageUtil.create(samePersonMuchAcctVos, pageNo, pageSize));
    }

    /**
     * 同法人、财务负责人拥有3个及以上的客户号 导出excel
     */
    //@RequiresPermissions("com.boc.ewr:cac:exportXls")
    @RequestMapping(value = "/exportXls/samePersonMuchAcct")
    public ModelAndView exportSamePersonMuchAcctXls() {
        String title = "同法人、财务负责人拥有3个及以上的客户号";

        List<SamePersonMuchAcctVo> samePersonMuchAcctVos = service.judgeSamePersonMuchAcct();

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, SamePersonMuchAcctVo.class);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + this.getLoginUser().getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, samePersonMuchAcctVos);
        return mv;
    }

    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
